package com.cms.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cms.domain.Feedback;
import com.cms.entity.FriendLink;
import com.cms.service.FriendLinkService;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;


/**
 * Controller - 友情链接
 * 
 * 
 * 
 */
@Controller("adminFriendLinkController")
@RequestMapping("/admin/friend_link")
public class FriendLinkController extends BaseController {

	@Autowired
	private FriendLinkService friendLinkService;

	/**
	 * 列表
	 */
	@RequestMapping
	public String index(String name, Integer pageNumber, ModelMap modelMap) {
		setListQuery();
		if(pageNumber==null){
			pageNumber = 1;
		}
		modelMap.addAttribute("page", friendLinkService.findPage(pageNumber,PAGE_SIZE,getCurrentSite().getId()));
		modelMap.addAttribute("gids", friendLinkService.findGids(getCurrentSite().getId()));
		modelMap.addAttribute("name", name);
		return getView("friend_link/index");
	}


	/**
	 * 添加
	 */
	@RequestMapping("/add")
	public String add(ModelMap modelMap) {
		modelMap.addAttribute("gids", friendLinkService.findGids(getCurrentSite().getId()));
		return getView("friend_link/add");
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public String save(FriendLink friendLink) {
		if(friendLink.getGid()==null){
			Integer maxGid = friendLinkService.findMaxGid(getCurrentSite().getId());
			if(maxGid==null){
				maxGid=0;
			}
			maxGid = maxGid+1;
			friendLink.setGid(maxGid);
		}
		friendLink.setSiteId(getCurrentSite().getId());
		friendLink.setCreateDate(LocalDateTime.now());
		friendLink.setUpdateDate(LocalDateTime.now());
		friendLinkService.save(friendLink);
		return "redirect:"+getListQuery("/admin/friend_link");
	}

	/**
	 * 编辑
	 */
	@RequestMapping("/edit")
	public String edit(Integer id,ModelMap modelMap) {
		modelMap.addAttribute("friendLink", friendLinkService.getById(id));
		modelMap.addAttribute("gids", friendLinkService.findGids(getCurrentSite().getId()));
		return getView("friend_link/edit");
	}

	/**
	 * 更新
	 */
	@RequestMapping("/update")
	public String update(FriendLink friendLink) {
		friendLink.setUpdateDate(LocalDateTime.now());
		friendLinkService.updateById(friendLink);
		return "redirect:"+getListQuery("/admin/friend_link");
	}
	
   /**
     * 修改排序
     */
   	@ResponseBody
   	@RequestMapping("/updateSort")
    public Feedback updateSort(String sortArray){
        JSONArray jsonArray = JSONArray.parseArray(sortArray);
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
			Integer id = jsonObject.getInteger("id");
            Integer sort = jsonObject.getInteger("sort");
            FriendLink friendLink = friendLinkService.getById(id);
            friendLink.setSort(sort);
			friendLinkService.updateById(friendLink);
        }
        return Feedback.success(new HashMap<>());
    }

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Feedback delete(Integer[] ids) {
		if(ArrayUtils.isNotEmpty(ids)){
			friendLinkService.removeByIds(Arrays.asList(ids));
		}
		return Feedback.success(new HashMap<>());
	}

}